import 'package:flutter/widgets.dart';

import 'random_joke_state.dart';

typedef RefreshJokeCallback = Future<void> Function();

/// Provides access to [RandomJokeState] and a refresh callback down the widget tree.
class RandomJokeScope extends InheritedWidget {
  const RandomJokeScope({
    super.key,
    required this.state,
    required this.refresh,
    required super.child,
  });

  final RandomJokeState state;
  final RefreshJokeCallback refresh;

  static RandomJokeScope of(BuildContext context) {
    final scope = context.dependOnInheritedWidgetOfExactType<RandomJokeScope>();
    assert(scope != null, 'RandomJokeScope not found in context');
    return scope!;
  }

  @override
  bool updateShouldNotify(RandomJokeScope oldWidget) =>
      oldWidget.state != state;
}
